package com.touchscope.tools;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import com.touchscope.activity.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class CheckboxAdapter extends BaseAdapter {

	Context context;
	ArrayList<HashMap<String, Object>> listData;
	public HashMap<Integer, Boolean> state = new HashMap<Integer, Boolean>();
	private Bitmap mIcon_folder;
	private Bitmap mIcon_doc;

	public CheckboxAdapter(Context context,
			ArrayList<HashMap<String, Object>> listData) {
		this.context = context;
		this.listData = listData;
		mIcon_folder = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.folder);
		mIcon_doc = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.doc);
	}

	// @Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listData.size();
	}

	// @Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listData.get(position);
	}

	// @Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	// @Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			LayoutInflater mInflater = LayoutInflater.from(context);
			convertView = mInflater.inflate(R.layout.item, null);
			ImageView image = (ImageView) convertView
					.findViewById(R.id.file_image);
			image.setBackgroundResource((Integer) listData.get(position).get(
					"file_image"));
			TextView username = (TextView) convertView
					.findViewById(R.id.file_name);
			username.setText((String) listData.get(position).get("file_name"));
			// TextView id = (TextView) convertView.findViewById(R.id.file_id);
			// id.setText((String) listData.get(position).get("friend_id"));
			CheckBox check = (CheckBox) convertView.findViewById(R.id.cb_selected);
			check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				// @Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					// TODO Auto-generated method stub
					if (isChecked) {
						state.put(position, isChecked);
					} else {
						state.remove(position);
					}
				}
			});
			check.setChecked((state.get(position) == null ? false : true));

			ViewHolder holder;
			holder = new ViewHolder();
			holder.file_name = (TextView) convertView
					.findViewById(R.id.file_name);
			holder.file_image = (ImageView) convertView
					.findViewById(R.id.file_image);
			convertView.setTag(holder);
			File f = new File(listData.get(position).get("file_id").toString());
			holder.file_name.setText(f.getName());
			if (f.isDirectory()) {
				holder.file_image.setImageBitmap(mIcon_folder);
			} else {
				holder.file_image.setImageBitmap(mIcon_doc);
			}
		}
		return convertView;
	}

	Handler myHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);

		}

	};
	public class ViewHolder {
		TextView file_name;
		ImageView file_image;
	}

}